package it.sources.user.controller;

import com.github.pagehelper.PageInfo;
import it.sources.user.domain.User;
import it.sources.user.service.IUserService;
import it.sources.user.domain.query.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import it.sources.untile.R;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService igs;

    @GetMapping
    List<User> findAll() {
        List<User> all = igs.findAll();
        return all;

    }
    //查询单个

    /**
     * 查询单个
     *
     * @return
     */
    @GetMapping("{id}")
    User findOne(@PathVariable("id") Long id) {
        User one = igs.findOne(id);
        return one;
    }
    //修改

    /**
     * 修改
     *
     * @return
     */
    @PutMapping
    R updete(@RequestBody User user) {
        try {
            if (user.getId() != null) {
                igs.upDate(user);
            } else {
                igs.add(user);
            }
            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }

    }
    //删除

    /**
     * 删除
     *
     * @return
     */
    @DeleteMapping("{id}")
    R delete(@PathVariable("id") Long id) {
        try {
            igs.delet(id);
            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }

    }
    @PatchMapping
        //批量删除
    R PathDelete(@RequestBody Long[]id){
        igs.pathDele(id);
        return  R.success();
    }
    //分页查询
    @PostMapping
    PageInfo<User> queryPage(@RequestBody UserQuery userQuery){
        PageInfo<User> PageInfo = igs.queryPage(userQuery);
        return PageInfo;
    }
}
